const path = require("path")            //  路径插件
const webpack = require("webpack")
const CleanWebpackPlugin = require("clean-webpack-plugin")      //  用于清空指定文件夹
const HtmlWebpackPlugin = require("html-webpack-plugin")        //  生成模板 index.html



function resolve(src) {
    return path.resolve(__dirname, src)
}

module.exports = {
    entry: {
        app: resolve('../src/index.js')
    },
    output: {
        filename: "[name].bundle.js",
        chunkFilename: '[name].chunk.[chunkhash].js',
        path: resolve("../dist"),
    },
    module: {
        rules: [
            {
                test: /\.(css)$/,
                use: [
                    'style-loader',
                    'css-loader'
                ]
            },
            {
                test: /\.(png|jpg|jpeg|gif|bmp)$/,
                use: {
                    loader: 'url-loader',
                    options: {
                        limit: 32000        //  单位 byte
                    }
                }
            },
            {
                test: /\.js$/,
                exclude: /node_modules/,
                loader: "babel-loader"
            }
        ]
    },
    resolve: {
        alias: {
            "@src": resolve("../src")     //  别名
        },
        extensions: ['.js']       //  可省略扩展名
    },
    optimization: {
        //  用于提取公共部分，形成 chunk
        splitChunks: {
            chunks: 'all'
        },
        // minimize: false     //  打包时不压缩代码
    },
    plugins: [
        new HtmlWebpackPlugin({
            title: "",
            filename: "index.html", //  打包后输出的文件名
            template: resolve('../template.html')   //  打包参照的模板，不配置这一项就自动生成
        }),
        new CleanWebpackPlugin(['dist'], {
            root: resolve('../')
        })
    ]
}
